package com.buluo.service.impl

import com.buluo.entity.Role
import com.buluo.service.ResourceService
import com.buluo.service.RoleService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 *
 * @author weiwei.huang
 * @email ihuangweiwei@163.com
 * @date 15/3/31
 */
@Service
class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

    static String NAMESPACE = (Role.class.getName() + ".").intern();

    @Autowired
    ResourceService resourceService


    @Override
    Set<String> findRoles(List<Integer> ids) {
        Set<String> roles = new HashSet<>()
        ids.each { roleId ->
            Role role = find(roleId)
            if (role) {
                roles.add(role.getName())
            }
        }
        return roles
    }

    @Override
    Set<String> findPermissions(List<Integer> ids) {
        Set<Integer> resourceIds = new HashSet<>()
        ids.each { roleId ->
            Role role = find(roleId)
            if (role != null) {
                resourceIds.addAll(role.getResourceIds())
            }
        }
        return this.resourceService.findPermissions(resourceIds)
    }

    @Override
    List<Role> findAll() {
        return getSqlSession().selectList(NAMESPACE + "findAll")
    }

    @Override
    void bindMenu(int id, String menuIds) {
        getSqlSession().update(NAMESPACE + "bindMenu", [resourceIds: menuIds, id: id])

    }

    @Override
    String getNameSpace() {
        return NAMESPACE
    }
}
